//
//  ViewController.m
//  IdeaShare_Mac_Demo
//
//  Created by IdeaShare on 2021/1/8.
//

#import "ViewController.h"
#import <IdeaShare_SDK/IdeaShareEventHandler.h>
static DiscoverResult* tempDiscoverResult = nil;
#define IDEA_SUCCESS 0
@interface ViewController ()<NSTextFieldDelegate>
@property(nonatomic,strong)NSControl * control;

@property (weak) IBOutlet NSTextField *nameLable;

@property (weak) IBOutlet NSTextField *portLable;

@property (weak) IBOutlet NSTextField *ipAddressLable;

@property (weak) IBOutlet NSTextField *versionLable;

@property (weak) IBOutlet NSTextField *codeTextField;

@property (weak) IBOutlet NSButton *connectButton;

@property (weak) IBOutlet NSButton *disconnectButton;

@property (weak) IBOutlet NSButton *startCaptureButton;

@property (weak) IBOutlet NSButton *stopCaptureButton;

@property (weak) IBOutlet NSButton *openMicrophoneButton;

@property (weak) IBOutlet NSButton *closeMicrophoneButton;

@property (weak) IBOutlet NSButton *openCameraButton;

@property (weak) IBOutlet NSButton *closeCarmeraButton;

@property (weak) IBOutlet NSButton *volumeIncreaseButton;

@property (weak) IBOutlet NSButton *volumeDecreaseButton;

@property (weak) IBOutlet NSButton *startAudioCastButton;

@property (weak) IBOutlet NSButton *stopAudioCastButton;

@property (weak) IBOutlet NSButton *infoButton;

@property (weak) IBOutlet NSButton *debugButton;

@property (weak) IBOutlet NSButton *blackCursor;

@property (weak) IBOutlet NSButton *lazerCursor;

@property (weak) IBOutlet NSButton *transparentCursor;

@property (weak) IBOutlet NSTextField *volumeNumberLable;

@property (weak) IBOutlet NSTextField *portTextField;

@property (weak) IBOutlet NSTextField *ipaddressTextField;

@property(nonatomic,copy)NSString * portString;

@property(nonatomic,copy)NSString * ipaddressString;

@property(nonatomic,copy)NSString * codeString;

@property(nonatomic,assign)NSInteger volumeNumber;

@property(nonatomic,strong)DiscoverResult * discoverResult;

@property (weak) IBOutlet NSButton *ParseCastCodeButton;

@property (nonatomic,strong) DeviceInfo * deviceInfo;

@property (weak) IBOutlet NSButton *leaveConferenceButton;

@property (weak) IBOutlet NSButton *finishConferenceButton;

@property (weak) IBOutlet NSButton *openSpeakerButton;

@property (weak) IBOutlet NSButton *closeSpeakerButton;

@property (nonatomic,strong) RemoteServiceStatus * remoteserviceStatus;

@property (weak) IBOutlet NSButton *remoterButton;

@property (weak) IBOutlet NSButton *left;

@property (weak) IBOutlet NSButton *right;

@property (nonatomic,strong) SharePolicy * sharepolicy;


@end

@implementation ViewController
- (DiscoverResult *)discoverResult {
    if (_discoverResult == nil) {
        _discoverResult = [[DiscoverResult alloc]init];
    }
    return _discoverResult;
}
- (SharePolicy *)sharepolicy {
    if (_sharepolicy == nil) {
        _sharepolicy =[[SharePolicy alloc]init];
        _sharepolicy.isShareAudio = YES;
        _sharepolicy.shareQuality = 2;
        _sharepolicy.isCompatibility = YES;
    }
    return _sharepolicy;
}
@synthesize mShareServiceController;

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([IdeaShareServiceController respondsToSelector:@selector(shareInstance)]) {
        mShareServiceController = [IdeaShareServiceController shareInstance];
    } else {
        NSLog(@"ResponseToSelector Failed");
    }
    mShareServiceController.notifyDelegate = (id)self;
    IdeaShareServiceLogInfo *logInfo = [[IdeaShareServiceLogInfo alloc]init];
    logInfo.enable = YES;
    logInfo.logLevel = 3;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *baseDir = paths.firstObject;
    NSString *logsDirectory = [baseDir stringByAppendingPathComponent:@"Logs"];
    logInfo.path = logsDirectory;
    int logRet = [mShareServiceController setLog:logInfo];
    NSLog(@"Set Log Result is %d",logRet);
    const IdeaShareServiceAppInfo *appInfo = [[IdeaShareServiceAppInfo alloc]init];
    appInfo.appInfo = @"aa";
    appInfo.exePath = @"bb";
    NSLog(@"AppInfo is %@",appInfo);
    NSLog(@"EXEPath length is %lu",(unsigned long)appInfo.exePath.length);
    int ret = [mShareServiceController create:appInfo];
    NSLog(@"Initialization Result is %d",ret);
    dispatch_async(dispatch_get_main_queue(), ^{
        self.versionLable.stringValue = [self->mShareServiceController getSDKVersion];
    });
    int shareResult = [mShareServiceController setPolicy:self.sharepolicy];
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(sleepReaction) name:NSWorkspaceWillSleepNotification object:nil];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.blackCursor.enabled = NO;
        self.lazerCursor.enabled = NO;
        self.transparentCursor.enabled = NO;
    });
    [self setupUI];
    
}

- (void) sleepReaction
{
    NSLog(@"Mac is Going to Sleep");
    int res = [mShareServiceController stopSharing:^(id  _Nonnull t) {
    }];
    if (res == IDEA_SUCCESS) {
        self.stopCaptureButton.enabled = NO;
        self.startCaptureButton.enabled = YES;
    }
}

- (void) onErrorNotify : (IDEA_SHARE_APP_ERR) errCode
{
    NSLog(@"APP Received ErrorCode - %ld",errCode);
    NSString * errTip = [[NSString alloc]initWithFormat:@"Error Notify - %@", [NSString stringWithFormat:@"%ld",errCode]];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self InformationTipsWithContext:errTip];
    });
    
}

- (void) onShareStatusChangeNotify : (IDEA_SHARE_APP_SERVER_STATUS) status : (IDEA_SHARE_APP_ERR) reason
{
    NSLog(@"Delegate Status is %ld, reason is %ld", (long)status, (long)reason);
        NSLog(@"Get Status Number is %ld",status);
        if (status == 3)
        {
            NSLog(@"After Share status equals 3");
            dispatch_async(dispatch_get_main_queue(), ^{
                self.sharepolicy.isShareAudio = NO;
                [self->mShareServiceController setPolicy:self.sharepolicy];
                self.discoverResult = nil;
                self.codeString = @"";
                self.startCaptureButton.enabled = NO;
                self.stopCaptureButton.enabled = NO;
                self.closeCarmeraButton.enabled = NO;
                self.closeMicrophoneButton.enabled = NO;
                self.closeSpeakerButton.enabled = NO;
                self.finishConferenceButton.enabled = NO;
                self.leaveConferenceButton.enabled = NO;
                self.openMicrophoneButton.enabled = NO;
                self.openCameraButton.enabled = NO;
                self.openSpeakerButton.enabled = NO;
                self.volumeIncreaseButton.enabled = NO;
                self.volumeDecreaseButton.enabled = NO;
                self.remoterButton.enabled = NO;
                self.codeTextField.stringValue = @"";
                self.nameLable.stringValue = @"<None>";
                self.startAudioCastButton.enabled = YES;
                self.stopAudioCastButton.enabled = NO;
            });
        } else if (status == 2) {
            NSLog(@"After Share status equals 2");
            dispatch_async(dispatch_get_main_queue(), ^{
                self.stopCaptureButton.enabled = NO;
                self.startCaptureButton.enabled = YES;
            });
        }
}

- (void) onChannelMsgHandler : (int) appId : (IdeaAppMsg *) msg
{
    NSLog(@"onChannelMsgHandler");
}

- (void) onDevConfStatusChangeNotify : (IDEA_SHARE_APP_CONF_STATUS) confStatus : (IDEA_SHARE_APP_AUX_STATUS) auxStatus
{
    NSLog(@"Delegate Conference Status is %ld, Aux Status is %ld",(long)confStatus, (long)auxStatus);
    if (confStatus == 1) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.leaveConferenceButton.enabled = YES;
        });
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.leaveConferenceButton.enabled = NO;
            self.finishConferenceButton.enabled = NO;
        });
    }
}

- (void) onServerMouseFeatureNotify : (int) mouseResult
{
    if (mouseResult == 1) {
        NSLog(@"The Server is available to set the style, [%d]", mouseResult);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.blackCursor.enabled = YES;
            self.lazerCursor.enabled = YES;
            self.transparentCursor.enabled = YES;
        });
    } else {
        NSLog(@"The Server is not available to set the style, [%d]", mouseResult);
    }
}

- (void) onRemoteServiceStatusChangedNotify : (RemoteServiceStatus *) remoteServiceStatus : (int) changeCount
{
    NSLog(@"onRemoteServiceStatusChangedNotigy");
    switch (changeCount) {
        case 16:
        {
            NSLog(@"OnRemoteMicStatusNotify: isMic = [%d]",remoteServiceStatus.isMute);
            if (remoteServiceStatus.isMute != 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.openMicrophoneButton.enabled = YES;
                    self.closeMicrophoneButton.enabled = NO;
                });
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.closeMicrophoneButton.enabled = YES;
                    self.openMicrophoneButton.enabled = NO;
                });
            }
            break;
        }
        case 8:
        {
            NSLog(@"OnRemoteCameraStatusNotify: isCameraMute = [%d]",remoteServiceStatus.isCameraMute);
            if (remoteServiceStatus.isCameraMute != 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.openCameraButton.enabled = YES;
                    self.closeCarmeraButton.enabled = NO;
                });
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.closeCarmeraButton.enabled = YES;
                    self.openCameraButton.enabled = NO;
                });
            }
            break;
        }
        case 2:
        {
            NSLog(@"OnRemoteSpeakerStatusNotify: isSpeakerMute = [%d]",remoteServiceStatus.isSpeakerMute);
            if (remoteServiceStatus.isSpeakerMute != 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.openSpeakerButton.enabled = YES;
                    self.closeSpeakerButton.enabled = NO;
                });
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.closeSpeakerButton.enabled = YES;
                    self.openSpeakerButton.enabled = NO;
                });
            }
            break;
        }
        case 1:
        {
            NSLog(@"OnRemoteChairManStatusNotify: isChairMan = [%d]", remoteServiceStatus.isChairman);
            if (remoteServiceStatus.isChairman == 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self InformationTipsWithContext:@"You are Not the ChairMan"];
                    self.finishConferenceButton.enabled = NO;
                });
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self InformationTipsWithContext:@"You are the ChairMan"];
                    self.leaveConferenceButton.enabled = YES;
                    self.finishConferenceButton.enabled = YES;
                });
            }
            break;
        }
        case 4:
        {
            NSLog(@"OnRemoteSpeakerVolumeStatusNotify: volume = [%d]", remoteServiceStatus.volume);
            dispatch_async(dispatch_get_main_queue(), ^{
                self.volumeNumber = remoteServiceStatus.volume;
                self.volumeNumberLable.stringValue = [NSString stringWithFormat:@"The Current Volume is : %ld",(long)remoteServiceStatus.volume];
            });
            break;
        }
    }
}

- (void)setupUI {
    self.codeTextField.maximumNumberOfLines = 1;
    self.portTextField.maximumNumberOfLines = 1;
    self.ipaddressTextField.maximumNumberOfLines = 1;
    [self.codeTextField.cell setScrollable:YES];
    [self.portTextField.cell setScrollable:YES];
    [self.ipaddressTextField.cell setScrollable:YES];
    self.codeTextField.delegate = self;
    self.portTextField.delegate = self;
    self.ipaddressTextField.delegate = self;
    self.closeCarmeraButton.enabled = NO;
    self.closeMicrophoneButton.enabled = NO;
    self.closeSpeakerButton.enabled = NO;
    self.finishConferenceButton.enabled = NO;
    self.leaveConferenceButton.enabled = NO;
    self.openMicrophoneButton.enabled = NO;
    self.openCameraButton.enabled = NO;
    self.openSpeakerButton.enabled = NO;
    self.volumeIncreaseButton.enabled = NO;
    self.volumeDecreaseButton.enabled = NO;
    self.startCaptureButton.enabled = NO;
    self.stopCaptureButton.enabled = NO;
    self.startAudioCastButton.enabled = NO;
    self.stopAudioCastButton.enabled = NO;
    
}

- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor {
    if (control.tag == 0) {
        self.portString = fieldEditor.string;
        NSLog(@"port-----%@",self.portString);
    } else if (control.tag == 1) {
        self.ipaddressString = fieldEditor.string;
        NSLog(@"ip-----%@",self.ipaddressString);
    } else {
        self.codeString = fieldEditor.string;
        NSLog(@"code-----%@",self.codeString);
    }
    return YES;
}

- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor {
    self.control = control;
    return YES;
}

- (IBAction)ParseCastCodeButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
//    IdeaShareServiceTlsInfo *tlsInfo = [[IdeaShareServiceTlsInfo alloc]init];
    NSString * castCode = self.codeString;
    NSString * address = self.ipaddressString;
    int port = [self.portString intValue];
    int result = [mShareServiceController discover:castCode withAddress:address withPort:port callback:^(id discoverCallback) {
            self.discoverResult = (DiscoverResult*)discoverCallback;
        NSLog(@"UI address is %@",self.discoverResult.address_list);
        }];
    NSLog(@"Outside password %@",tempDiscoverResult.password);
    NSLog(@"dicover result is %d",result);
    if (result == 0)
    {
        NSLog(@"discover success");
    }
    else{
        NSLog(@"discover failed");
    }
}


- (IBAction)remoterButtonClick:(id)sender
{
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"remoterOpen");
    self.stopAudioCastButton.enabled = YES;
    int result = [mShareServiceController getRemoteServiceStatus:^(id status) {
        if (status == nil) {
            NSLog(@"status is nil");
        } else {
            self.remoteserviceStatus = (RemoteServiceStatus *)status;
            dispatch_async(dispatch_get_main_queue(), ^{
                self.volumeDecreaseButton.enabled = YES;
                self.volumeIncreaseButton.enabled = YES;
                self.volumeNumber = self.remoteserviceStatus.volume;
                self.volumeNumberLable.stringValue = [NSString stringWithFormat:@"The Current Volume is : %ld",(long)self.remoteserviceStatus.volume];
            });
            if (self.remoteserviceStatus.isMute != 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.openMicrophoneButton.enabled = YES;
                    self.closeMicrophoneButton.enabled = NO;
                });
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.closeMicrophoneButton.enabled = YES;
                    self.openMicrophoneButton.enabled = NO;
                });
            }
            if (self.remoteserviceStatus.isSpeakerMute != 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.openSpeakerButton.enabled = YES;
                    self.closeSpeakerButton.enabled = NO;
                });
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.closeSpeakerButton.enabled = YES;
                    self.openSpeakerButton.enabled = NO;
                });
            }
            
            if (self.remoteserviceStatus.isBase == 1) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.openCameraButton.enabled = NO;
                    self.closeCarmeraButton.enabled = NO;
                });
            } else {
                if (self.remoteserviceStatus.isCameraMute != 0) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        self.openCameraButton.enabled = YES;
                        self.closeCarmeraButton.enabled = NO;
                    });
                } else {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        self.closeCarmeraButton.enabled = YES;
                        self.openCameraButton.enabled = NO;
                    });
                }
            }
            
        }
        
        
    }];
}


- (IBAction)leaveConferenceButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----%@",__FUNCTION__,self.codeString);
    NSLog(@"leave conference");
    int result = [mShareServiceController leaveConference:^(id leaveConferencecallback) {
    }];
    NSLog(@"leave conference result is %d",result);
    if (result == IDEA_SUCCESS)
    {
        NSLog(@"leave conference successful");
    }
    else{
        NSLog(@"leave conference failed");
    }
}

- (IBAction)finishConferenceButtonClick:(id)sender{
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    NSLog(@"finish conference");
    int result = [mShareServiceController finishConference:^(id t) {
    }];
    NSLog(@"finish conference result is %d",result);
    if (result == IDEA_SUCCESS)
    {
        NSLog(@"finish conference successful");
    }
    else
    {
        NSLog(@"finish conference failed");
    }
}

- (IBAction)connectButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    if (self.discoverResult.address_list == nil || self.discoverResult.password == nil) {
        NSLog(@"Please Click [Parse Code] Firstly");
    } else {
        NSMutableArray * addr_list = self.discoverResult.address_list;
        NSString * password = self.discoverResult.password;
        NSLog(@"IP:%@, password:%@",addr_list,password);
        int port = 1444;
        int connectResult = [mShareServiceController connect:addr_list withPort:port withPassword :password callback :^(id connectCallback) {
            self.deviceInfo = (DeviceInfo*)connectCallback;
            dispatch_async(dispatch_get_main_queue(),^(){
                self.nameLable.stringValue = self.deviceInfo.deviceName;
                self.startCaptureButton.enabled = YES;
                self.remoterButton.enabled = YES;
            });
        }];
        if (connectResult == IDEA_SUCCESS) {
            self.sharepolicy.isShareAudio = YES;
            [mShareServiceController setPolicy:self.sharepolicy];
            self.stopAudioCastButton.enabled = YES;
            [mShareServiceController getRemoteServiceStatus:^(id status) {
                if (status == nil) {
                    NSLog(@"status is nil");
                } else {
                    self.remoteserviceStatus = (RemoteServiceStatus *)status;
                    if (self.remoteserviceStatus.isChairman == 1) {
                        dispatch_async(dispatch_get_main_queue(),^(){
                        self.finishConferenceButton.enabled = YES;
                        });
                    } else {
                        dispatch_async(dispatch_get_main_queue(),^(){
                        self.finishConferenceButton.enabled = NO;
                        });
                    }
                }
            }];
            IdeaAppTableKey *appKey = [[IdeaAppTableKey alloc]init];
            appKey.appNameKey = @"HuaweiCloud";
            int result = [mShareServiceController channelRegisterApp:appKey];
            if (result != IDEA_SUCCESS) {
                NSLog(@"channelRegisterApp failed");
            } else {
                NSString *str = @"Hello";
                NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
                int res = [mShareServiceController channelSendMsg:2:data:data.length];
            }
        }
        NSLog(@"connect result is %d",connectResult);
    }
}
- (IBAction)disconnectButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    __block int reason;
    int result = [mShareServiceController disconnect:^(id disconnectCallback) {
        reason = (int)disconnectCallback;
        NSLog(@"Disconnect Reason is %d",reason);
        IdeaAppTableKey *appKey = [[IdeaAppTableKey alloc]init];
        appKey.appNameKey = @"HuaweiCloud";
        int res = [mShareServiceController channelUnRegisterApp:appKey];
        if (res != IDEA_SUCCESS) {
            NSLog(@"");
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            self.sharepolicy.isShareAudio = NO;
            [mShareServiceController setPolicy:self.sharepolicy];
            self.discoverResult = nil;
            self.codeString = @"";
            self.startCaptureButton.enabled = NO;
            self.stopCaptureButton.enabled = NO;
            self.closeCarmeraButton.enabled = NO;
            self.closeMicrophoneButton.enabled = NO;
            self.closeSpeakerButton.enabled = NO;
            self.finishConferenceButton.enabled = NO;
            self.leaveConferenceButton.enabled = NO;
            self.openMicrophoneButton.enabled = NO;
            self.openCameraButton.enabled = NO;
            self.openSpeakerButton.enabled = NO;
            self.volumeIncreaseButton.enabled = NO;
            self.volumeDecreaseButton.enabled = NO;
            self.remoterButton.enabled = NO;
            self.codeTextField.stringValue = @"";
            self.nameLable.stringValue = @"<None>";
            self.stopAudioCastButton.enabled = NO;
            self.startAudioCastButton.enabled = NO;
        });
    }];
    NSLog(@"Disconnect result is %d, and reason is %d",result,reason);
}
- (void)controlTextDidChange:(NSNotification *)obj {
    NSText * fileldEditor = [[obj userInfo] objectForKey:@"NSFieldEditor"];
    if (self.control.tag == 0) {
        NSLog(@"port-----");
    } else if (self.control.tag == 1) {
        NSLog(@"IP-----");
    } else {
        [fileldEditor setString:[[fileldEditor string] uppercaseString]];
    }
}

- (IBAction)startCaptureButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    int result = [mShareServiceController startSharing:^(id startSharingCallback) {
    }];
    if (result == IDEA_SUCCESS)
    {
        self.startCaptureButton.enabled = NO;
        self.stopCaptureButton.enabled = YES;
    }
}
- (IBAction)stopCaptureButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    __block int reason;
    int result = [mShareServiceController stopSharing:^(id stopSharingCallback) {
        reason = (int)stopSharingCallback;
    }];
    if (result == IDEA_SUCCESS)
    {
        self.stopCaptureButton.enabled = NO;
        self.startCaptureButton.enabled = YES;
    }
    NSLog(@"stop Sharing result is %d, reason is %d",result, reason);
}
- (IBAction)openMicrophoneButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    int result = [mShareServiceController setRemoteMicMute:false callback:^(id micOpenCallback) {
    }];
    if (result != 0)
    {
        NSLog(@"open Microphone failed");
    }
    else{
        NSLog(@"open Microphone success");
        self.closeMicrophoneButton.enabled = YES;
        self.openMicrophoneButton.enabled = NO;
    }
}
- (IBAction)closeMicrophoneButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    int result = [mShareServiceController setRemoteMicMute:true callback:^(id micCloseCallback) {
    }];
    if (result != 0)
    {
        NSLog(@"close Microphone failed");
    }
    else{
        NSLog(@"close Microphone success");
        self.closeMicrophoneButton.enabled = NO;
        self.openMicrophoneButton.enabled = YES;
    }
}
- (IBAction)openCameraButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    int result = [mShareServiceController setRemoteCameraMute:false callback:^(id cameraOpenCallback) {
    }];
    if (result != 0)
    {
        NSLog(@"open Camera failed");
    }
    else{
        NSLog(@"open Camera success");
        self.closeCarmeraButton.enabled = YES;
        self.openCameraButton.enabled = NO;
    }
}
- (IBAction)closeCarmeraButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    int result = [mShareServiceController setRemoteCameraMute:true callback:^(id cameraCloseCallback) {
    }];
    if (result != 0)
    {
        NSLog(@"close Camera failed");
    }
    else{
        NSLog(@"close Camera success");
        self.closeCarmeraButton.enabled = NO;
        self.openCameraButton.enabled = YES;
    }
}

- (IBAction)openSpeakerButtonClick:(id)sender
{
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    int result;
    if (self.volumeNumber == 0) {
        self.volumeNumber = 1;
        result = [mShareServiceController setSpeakerVolume:self.volumeNumber callback:^(id volumecallback) {
        }];
    } else {
        result = [mShareServiceController setRemoteSpeakerMute:false callback:^(id speakerOpenCallback) {
        }];
    }
    if (result != IDEA_SUCCESS)
    {
        NSLog(@"open Speaker failed");
    }
    else{
        NSLog(@"open Speaker success");
        self.closeSpeakerButton.enabled = YES;
        self.openSpeakerButton.enabled = NO;
    }
}

- (IBAction)closeSpeakerButtonClick:(id)sender
{
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    int result = [mShareServiceController setRemoteSpeakerMute:true callback:^(id speakerCloseCallback) {
    }];
    if (result != IDEA_SUCCESS)
    {
        NSLog(@"close Speaker failed");
    }
    else{
        NSLog(@"close Speaker success");
        self.closeSpeakerButton.enabled = NO;
        self.openSpeakerButton.enabled = YES;
    }
}

- (IBAction)volumeIncreaseButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    self.volumeNumber++;
    if (self.volumeNumber > 21) {
        self.volumeNumber = 21;
    } else if (self.volumeNumber < 0) {
        self.volumeNumber = 0;
    }
    self.volumeNumberLable.stringValue = [NSString stringWithFormat:@"The Current Volume is : %ld",(long)self.volumeNumber];
    NSLog(@"%s-----",__FUNCTION__);
    [mShareServiceController setSpeakerVolume:self.volumeNumber callback:^(id volumecallback) {
    }];
}
- (IBAction)volumeDecreaseButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    self.volumeNumber--;
    if (self.volumeNumber > 21) {
        self.volumeNumber = 21;
    } else if (self.volumeNumber < 0) {
        self.volumeNumber = 0;
    }
    self.volumeNumberLable.stringValue = [NSString stringWithFormat:@"The Current Volume is : %ld",(long)self.volumeNumber];
    NSLog(@"%s-----",__FUNCTION__);
    [mShareServiceController setSpeakerVolume:self.volumeNumber callback:^(id volumecallback) {
    }];
}
- (IBAction)startAudioCastButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    self.sharepolicy.isShareAudio = YES;
    NSLog(@"Demo Side isShareAudio : %d, shareQuality : %ld",self.sharepolicy.isShareAudio,(long)self.sharepolicy.shareQuality);
    int result = [mShareServiceController setPolicy:self.sharepolicy];
    if (result != IDEA_SUCCESS)
    {
        NSLog(@"start Audio Failed");
    } else {
        NSLog(@"start Audio success");
        self.startAudioCastButton.enabled = NO;
        self.stopAudioCastButton.enabled = YES;
    }
}
- (IBAction)stopAudioCastButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    self.sharepolicy.isShareAudio = NO;
    int result = [mShareServiceController setPolicy:self.sharepolicy];
    if (result != IDEA_SUCCESS)
    {
        NSLog(@"stop Audio failed");
    } else {
        NSLog(@"stop Audio success");
        self.stopAudioCastButton.enabled = NO;
        self.startAudioCastButton.enabled = YES;
    }
}

- (IBAction)leftButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    IdeaShareServiceLogInfo *logInfo = [[IdeaShareServiceLogInfo alloc]init];
    logInfo.enable = YES;
    logInfo.logLevel = 0;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *baseDir = paths.firstObject;
    NSString *logsDirectory = [baseDir stringByAppendingPathComponent:@"Logs"];
    logInfo.path = logsDirectory;
    int logRet = [mShareServiceController setLog:logInfo];
    NSLog(@"Set Log Result is %d",logRet);
//    int res = [mShareServiceController destroy];
////    const IdeaShareServiceAppInfo *appInfoTwo = nil;
////    int res = [mShareServiceController create:appInfoTwo];
//    NSLog(@"Destroy Twice Res is %d",res);
//    [mShareServiceController getSDKLatestVersion:^(id  _Nonnull t) {
//        NSLog(@"The Version is %@", t);
//    }];
}

- (IBAction)rightButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    IdeaShareServiceLogInfo *logInfo = [[IdeaShareServiceLogInfo alloc]init];
    logInfo.enable = YES;
    logInfo.logLevel = 1;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *baseDir = paths.firstObject;
    NSString *logsDirectory = [baseDir stringByAppendingPathComponent:@"Logs"];
    logInfo.path = logsDirectory;
    int logRet = [mShareServiceController setLog:logInfo];
    NSLog(@"Set Log Result is %d",logRet);
}

- (IBAction)infoButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    IdeaShareServiceLogInfo *logInfo = [[IdeaShareServiceLogInfo alloc]init];
    logInfo.enable = YES;
    logInfo.logLevel = 2;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *baseDir = paths.firstObject;
    NSString *logsDirectory = [baseDir stringByAppendingPathComponent:@"Logs"];
    logInfo.path = logsDirectory;
    int logRet = [mShareServiceController setLog:logInfo];
    NSLog(@"Set Log Result is %d",logRet);
}

- (IBAction)debugButtonClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    IdeaShareServiceLogInfo *logInfo = [[IdeaShareServiceLogInfo alloc]init];
    logInfo.enable = YES;
    logInfo.logLevel = 3;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *baseDir = paths.firstObject;
    NSString *logsDirectory = [baseDir stringByAppendingPathComponent:@"Logs"];
    logInfo.path = logsDirectory;
    int logRet = [mShareServiceController setLog:logInfo];
    NSLog(@"Set Log Result is %d",logRet);
}

- (IBAction)blackCursorClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    int result = [mShareServiceController setMouseShape:253];
    NSLog(@"Set blackCursor is %d",result);
}

- (IBAction)lazerCursorClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    int result = [mShareServiceController setMouseShape:255];
    NSLog(@"Set lazerCursor is %d",result);
}

- (IBAction)transparentCursorClick:(id)sender {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
    NSLog(@"%s-----",__FUNCTION__);
    int result = [mShareServiceController setMouseShape:0];
    NSLog(@"Set blackCursor is %d",result);
}

- (void)InformationTipsWithContext:(NSString *)tipsString {
    NSAlert * alert = [[NSAlert alloc]init];
    [alert addButtonWithTitle:@"OK"];
    [alert setMessageText:@"Status Tips"];
    [alert setInformativeText:tipsString];
    [alert setAlertStyle:NSAlertStyleWarning];
    [alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode) {
        if (returnCode == NSAlertFirstButtonReturn) {
            NSLog(@"ok-------");
        }
    }];
}
- (void)mouseDown:(NSEvent *)event {
    [[NSApplication sharedApplication].mainWindow makeFirstResponder:0];
}
@end
